//
//  HomeFlowStoreCatCell.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/13.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "HomeFlowStoreCatCell.h"
#import "StoreCategoryCollectionCell.h"

@interface HomeFlowStoreCatCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>{
    
    CGSize _cellSize;
    UIEdgeInsets _collectionEdge;
}

@property (nonatomic, strong) UICollectionView *collectionview;

@end

@implementation HomeFlowStoreCatCell

+ (CGFloat)calculateHeight:(NSInteger)count {
    if (count == 0) {
        return 0;
    }
    CGFloat collectionHeight = 28 + 7.5 + 15;
    NSInteger column = count % 5 == 0 ? count / 5 : (count / 5) + 1;
    CGFloat totalHeight = (collectionHeight * column ) + (column > 0 ? ((column - 1) * 10.0) : 0);
    totalHeight += 10;
    return totalHeight;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self layout];
    }
    return self;
}

- (void)layout {
    [self addSubview:self.collectionview];
    [self.collectionview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.bottom.mas_equalTo(0);
    }];
}

- (void)fillCellWithModel:(HomeADItemModel *)model {
    
    self.dataArray = @[].mutableCopy;
    self.dataArray = model.sysAdvertisement.sysAdvEventList;
    [self.collectionview reloadData];
}

- (void)layoutSubviews {
    [super layoutSubviews];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    StoreCategoryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StoreCategoryCollectionCellID" forIndexPath:indexPath];
    cell.data = self.dataArray[indexPath.row];
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [[StatisticManager shareManager]insertBrowseUserActiveQuantityWithType:@"3"];
    if (self.HomeFlowStoreCatCellBlock) {
        self.HomeFlowStoreCatCellBlock(indexPath.row);
    }
}

- (UICollectionView *)collectionview {
    if (!_collectionview) {
        CGFloat width = TTScreenWidth - 20;
        
        CGFloat cellWidth, cellHeight;
        cellWidth = width/5;
        cellHeight = 28 + 7.5 + 15;
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 10;
        flowLayout.itemSize = CGSizeMake(cellWidth, cellHeight);

        _collectionview  = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
        _collectionview.delegate = self;
        _collectionview.dataSource = self;
        _collectionview.pagingEnabled = YES;
        _collectionview.showsHorizontalScrollIndicator = NO;
        _collectionview.showsVerticalScrollIndicator = NO;
        _collectionview.backgroundColor = [UIColor whiteColor];
        [_collectionview registerClass:[StoreCategoryCollectionCell class] forCellWithReuseIdentifier:@"StoreCategoryCollectionCellID"];
    }
    return _collectionview;
}

@end

